﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace MC34063_Universal_Calculator
{
    public static class DefaultValuesController
    {
        public static DefaultValues Load(string filename)
        {
            DefaultValues dv = null;
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                dv = Load(fs);
                fs.Close();
            }
            return dv;
        }

        public static DefaultValues Load(Stream fs)
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(DefaultValues));
            DefaultValues dv = (DefaultValues)xmlSer.Deserialize(fs);
            return dv;
        }

        public static void Save(DefaultValues dv, string filename)
        {
            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
            {
                Save(dv, fs);
                fs.Close();
            }
        }

        public static void Save(DefaultValues dv, Stream fs)
        {
            using (XmlTextWriter xmlWriter = new XmlTextWriter(fs, Encoding.UTF8))
            {
                xmlWriter.Formatting = Formatting.Indented;
                XmlSerializer xmlSer = new XmlSerializer(typeof(DefaultValues));
                xmlSer.Serialize(xmlWriter, dv);
            }
        }
    }

    [Serializable]
    [XmlRootAttribute("MC34063_UC_DefaultValues")]
    public class DefaultValues
    {
        ICValue[] _icValues;
        DiodeValue[] _diodeValues;
        TolValue[] _rtolValues;
        TolValue[] _ctolValues;

        public DefaultValues()
        {
        }

        [XmlArrayAttribute("ICValues")]
        [XmlArrayItem("ICValue")]
        public ICValue[] ICValues
        {
            get
            {
                return _icValues;
            }
            set
            {
                _icValues = value;
            }
        }

        [XmlArrayAttribute("DiodeValues")]
        [XmlArrayItem("DiodeValue")]
        public DiodeValue[] DiodeValues
        {
            get
            {
                return _diodeValues;
            }
            set
            {
                _diodeValues = value;
            }
        }

        [XmlArrayAttribute("RTolValues")]
        [XmlArrayItem("RTolValue")]
        public TolValue[] RTolValues
        {
            get
            {
                return _rtolValues;
            }
            set
            {
                _rtolValues = value;
            }
        }

        [XmlArrayAttribute("CTolValues")]
        [XmlArrayItem("CTolValue")]
        public TolValue[] CTolValues
        {
            get
            {
                return _ctolValues;
            }
            set
            {
                _ctolValues = value;
            }
        }
    }
}

//(c) 2011 - Tobias Eiseler

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>